package com.imachen.ymcc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.imachen.ymcc.domain.Login;
import com.imachen.ymcc.dto.LoginDto;
import com.imachen.ymcc.dto.RefreshDto;
import com.imachen.ymcc.enums.GlobalExceptionCode;
import com.imachen.ymcc.mapper.LoginMapper;
import com.imachen.ymcc.result.JSONResult;
import com.imachen.ymcc.service.ILoginService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.imachen.ymcc.util.AssertUtil;
import com.imachen.ymcc.util.HttpUtil;
import com.imachen.ymcc.vo.LoginVo;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author imachen
 * @since 2022-09-23
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    private static final String LOGIN_URL = "http://localhost:10030/oauth/token?client_id=%s&client_secret=%s&grant_type=password&username=%s&redirect_uri=http://www.baidu.com&password=%s";
    private static final String REFRESH_URL = "http://localhost:10030/oauth/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s";
    /**
     * 账号密码登录
     * @param loginDto
     * @return
     */
    @Override
    public JSONResult common(LoginDto loginDto) {
        String username = loginDto.getUsername();
        String password = loginDto.getPassword();
        Integer type = loginDto.getType();

        String clientId = "";
        String clientSecret = "";
        if (type == 0){ // 后台
            clientId = "admin";
            clientSecret = "123";
        }else if (type == 1){ // 前台
            clientId = "website";
            clientSecret = "123";
        }
        AssertUtil.isNotEmpty(clientId , GlobalExceptionCode.ERROR);
        String loginUrl = String.format(LOGIN_URL, clientId, clientSecret, username, password);
        String result = HttpUtil.sendPost(loginUrl, null);

        if ( result.contains("error_description") ) {
            JSONObject jsonObject = JSON.parseObject(result, JSONObject.class);
            return JSONResult.error(jsonObject.getString("error_description"));
        }

        LoginVo loginVo = JSON.parseObject(result, LoginVo.class);
        loginVo.setExpiresTime(loginVo.getExpires_in() + System.currentTimeMillis());

        return JSONResult.success(loginVo);
    }

    /**
     * 刷新Token
     * @param refreshDto
     * @return
     */
    @Override
    public JSONResult refresh(RefreshDto refreshDto) {
        Integer type = refreshDto.getType();
        String refreshToken = refreshDto.getRefreshToken();

        String clientId = "";
        String clientSecret = "";
        if (type == 0){ // 后台
            clientId = "admin";
            clientSecret = "123";
        }else if (type == 1){ // 前台
            clientId = "website";
            clientSecret = "123";
        }
        AssertUtil.isNotEmpty(clientId , GlobalExceptionCode.ERROR);
        String loginUrl = String.format(REFRESH_URL, refreshToken, clientId, clientSecret);
        String result = HttpUtil.sendPost(loginUrl, null);

        if ( result.contains("error_description") ) {
            JSONObject jsonObject = JSON.parseObject(result, JSONObject.class);
            return JSONResult.error(jsonObject.getString("error_description"));
        }

        LoginVo loginVo = JSON.parseObject(result, LoginVo.class);
        loginVo.setExpiresTime(loginVo.getExpires_in() + System.currentTimeMillis());

        return JSONResult.success(loginVo);
    }
}
